package org.nfyszny.zzzs.modules.controller;

import java.util.Arrays;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoDict;
import org.jeecg.common.system.query.QueryGenerator;
import org.nfyszny.zzzs.modules.constant.CertificateConstant;
import org.nfyszny.zzzs.modules.entity.ZzzsCertificate;
import org.nfyszny.zzzs.modules.service.IZzzsCertificateService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

/**
 * @Description: 证书表
 * @Author: jeecg-boot
 * @Date: 2022-11-11
 * @Version: V1.0
 */
@Api(tags = "证书表")
@RestController
@RequestMapping("/modules/zzzsCertificate")
@Slf4j
public class ZzzsCertificateController extends JeecgController<ZzzsCertificate, IZzzsCertificateService> {
    @Autowired
    private IZzzsCertificateService zzzsCertificateService;




    @ApiOperation(value = "证书表-证书年审", notes = "证书表-证书年审")
    @GetMapping(value = "/listReview")
    @AutoDict
    public Result<IPage<ZzzsCertificate>> listReview(ZzzsCertificate zzzsCertificate,
                                                     @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                     @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                     HttpServletRequest req) {
        QueryWrapper<ZzzsCertificate> queryWrapper = QueryGenerator.initQueryWrapper(zzzsCertificate, req.getParameterMap());
        Page<ZzzsCertificate> page = new Page<ZzzsCertificate>(pageNo, pageSize);
        IPage<ZzzsCertificate> pageList = zzzsCertificateService.setListByStatus(page, queryWrapper, CertificateConstant.CertificateStatus.SUSPENDED.getCode());
        return Result.TOK(pageList);
    }

    @ApiOperation(value = "证书表-证书注销", notes = "证书表-证书注销")
    @GetMapping(value = "/listLogout")
    @AutoDict
    public Result<IPage<ZzzsCertificate>> listLogout(ZzzsCertificate zzzsCertificate,
                                                     @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                     @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                     HttpServletRequest req) {
        QueryWrapper<ZzzsCertificate> queryWrapper = QueryGenerator.initQueryWrapper(zzzsCertificate, req.getParameterMap());

        Page<ZzzsCertificate> page = new Page<ZzzsCertificate>(pageNo, pageSize);
        IPage<ZzzsCertificate> pageList = zzzsCertificateService.setListByStatus(page, queryWrapper, CertificateConstant.CertificateStatus.GIVEN_OUT.getCode(), CertificateConstant.CertificateStatus.INVALID.getCode());


        return Result.TOK(pageList);
    }

    @AutoLog(value = "证书表-获取证书状态")
    @ApiOperation(value = "证书表-获取证书状态", notes = "证书表-获取证书状态")
    //@RequiresPermissions("org.nfyszny.zzzs:zzzs_record_enterprise:add")
    @GetMapping(value = "/getStatus/{id}")
    public Result add(@PathVariable("id") String id) {
        Boolean status = zzzsCertificateService.getStatus(null, true);
        return Result.TOK(status);
    }

    @ApiOperation(value = "证书表-证书变更", notes = "证书表-证书变更")
    @GetMapping(value = "/listChange")
    @AutoDict
    public Result<IPage<ZzzsCertificate>> listChange(ZzzsCertificate zzzsCertificate,
                                                     @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                     @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                     HttpServletRequest req) {
        QueryWrapper<ZzzsCertificate> queryWrapper = QueryGenerator.initQueryWrapper(zzzsCertificate, req.getParameterMap());

        Page<ZzzsCertificate> page = new Page<ZzzsCertificate>(pageNo, pageSize);
        IPage<ZzzsCertificate> pageList = zzzsCertificateService.setListByStatus(page, queryWrapper, CertificateConstant.CertificateStatus.GIVEN_OUT.getCode());
        return Result.TOK(pageList);
    }

    /**
     * 分页列表查询
     *
     * @param zzzsCertificate
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "证书表-分页列表查询")
    @ApiOperation(value = "证书表-分页列表查询", notes = "证书表-分页列表查询")
    @GetMapping(value = "/list")
    @AutoDict
    public Result<IPage<ZzzsCertificate>> queryPageList(ZzzsCertificate zzzsCertificate,
                                                        @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                        @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                        HttpServletRequest req) {
        QueryWrapper<ZzzsCertificate> queryWrapper = QueryGenerator.initQueryWrapper(zzzsCertificate, req.getParameterMap());
        Page<ZzzsCertificate> page = new Page<ZzzsCertificate>(pageNo, pageSize);
        IPage<ZzzsCertificate> pageList = zzzsCertificateService.page(page, queryWrapper);
        return Result.TOK(pageList);
    }

    /**
     * 添加
     *
     * @param zzzsCertificate
     * @return
     */
    @AutoLog(value = "证书表-添加")
    @ApiOperation(value = "证书表-添加", notes = "证书表-添加")
    //@RequiresPermissions("org.nfyszny.zzzs:zzzs_certificate:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody ZzzsCertificate zzzsCertificate) {
        zzzsCertificateService.save(zzzsCertificate);
        return Result.TOK("添加成功！");
    }

    /**
     * 注销证书
     */
    @AutoLog(value = "注销证书-编辑")
    @ApiOperation(value = "注销证书-编辑", notes = "注销证书-编辑")
    //@RequiresPermissions("org.nfyszny.zzzs:zzzs_certificate:edit")
    @RequestMapping(value = "/cancellationCertificate", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> cancellationCertificate(@RequestBody ZzzsCertificate zzzsCertificate) {
        zzzsCertificateService.cancellationCertificate(zzzsCertificate);
        return Result.TOK("编辑成功!");
    }

    /**
     * 暂停证书
     * @param zzzsCertificate
     * @return
     */
    @AutoLog(value = "暂停证书-编辑")
    @ApiOperation(value = "暂停证书-编辑", notes = "暂停证书-编辑")
    //@RequiresPermissions("org.nfyszny.zzzs:zzzs_certificate:edit")
    @RequestMapping(value = "/suspend", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> suspend(@RequestBody ZzzsCertificate zzzsCertificate) {
        zzzsCertificateService.suspend(zzzsCertificate);
        return Result.TOK("编辑成功!");
    }

    /**
     * 编辑
     *
     * @param zzzsCertificate
     * @return
     */
    @AutoLog(value = "证书表-编辑")
    @ApiOperation(value = "证书表-编辑", notes = "证书表-编辑")
    //@RequiresPermissions("org.nfyszny.zzzs:zzzs_certificate:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody ZzzsCertificate zzzsCertificate) {
        zzzsCertificateService.updateById(zzzsCertificate);
        return Result.TOK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "证书表-通过id删除")
    @ApiOperation(value = "证书表-通过id删除", notes = "证书表-通过id删除")
    //@RequiresPermissions("org.nfyszny.zzzs:zzzs_certificate:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        zzzsCertificateService.removeById(id);
        return Result.TOK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "证书表-批量删除")
    @ApiOperation(value = "证书表-批量删除", notes = "证书表-批量删除")
    //@RequiresPermissions("org.nfyszny.zzzs:zzzs_certificate:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.zzzsCertificateService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.TOK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "证书表-通过id查询")
    @ApiOperation(value = "证书表-通过id查询", notes = "证书表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<ZzzsCertificate> queryById(@RequestParam(name = "id", required = true) String id) {
        ZzzsCertificate zzzsCertificate = zzzsCertificateService.getById(id);
        if (zzzsCertificate == null) {
            return Result.error("未找到对应数据");
        }
        return Result.TOK(zzzsCertificate);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param zzzsCertificate
     */
    //@RequiresPermissions("org.nfyszny.zzzs:zzzs_certificate:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, ZzzsCertificate zzzsCertificate) {
        return super.exportXls(request, zzzsCertificate, ZzzsCertificate.class, "证书表");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    //@RequiresPermissions("zzzs_certificate:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, ZzzsCertificate.class);
    }

}
